package com.jackstraw.jack_base.util.loader.beanloader;


import com.jackstraw.jack_base.util.SpringUtil;
import com.jackstraw.jack_base.util.StringUtil;
import com.jackstraw.jack_base.util.loader.classloader.DynamicClass;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DynamicBean {

    private DynamicClass dynamicClass;

    public DynamicBean(DynamicClass dynamicClass) {
        this.dynamicClass = dynamicClass;
    }

    public static DynamicBean init(DynamicClass dynamicClass) {
        return new DynamicBean(dynamicClass);
    }

    public String load() {
        //销毁Bean
        SpringUtil.destroy(dynamicClass.getFullClassName());
        //每次都是new新的ClassLoader对象
        Class<?> type = dynamicClass.compiler().load();
        SpringUtil.registerSingleton(type);
        return StringUtil.beanName(dynamicClass.getFullClassName());
    }
}
